// tabBarComponent/tabBar.js
var util = require('../utils/util.js');
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabbar: {
      type: Object,
      value: {
        "backgroundColor": "#ffffff",
        "color": "#E85850",
        "selectedColor": "#1c1c1b",
        "list": [
          // {
          //   "pagePath": "/pages/index/index",
          //   "iconPath": "icon/icon_home.png",
          //   "selectedIconPath": "icon/icon_home_HL.png",
          //   "text": "首页"
          // },
          // {
          //   "pagePath": "/pages/create/create",
          //   "iconPath": "icon/icon_release.png",
          //   "isSpecial": true,
          //   "text": "发起抽奖"
          // },
          // {
          //   "pagePath": "/pages/mine/mine",
          //   "iconPath": "icon/icon_mine.png",
          //   "selectedIconPath": "icon/icon_mine_HL.png",
          //   "text": "我的"
          // }
        ]
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isIphoneX: app.globalData.systemInfo.model == "iPhone X" ? true : false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
  // 获取用户授权

  /**
   * 组件的方法列表
   */
  methods: {
    bindGetUserInfo: function(e) {
      var index = e.target.dataset.index;
      var getUserInfo = app.globalData.getUserInfo;
      if (e.detail.userInfo && getUserInfo == '') {//用户第一次点击，去登录授权
	  wx.showToast({
		  title: '加载数据中',
		  icon:'none',
		  duration: 50000
	  })
        // 用户按了允许授权按钮
        // 获取需要上传的数据
        var userInfo = e.detail.userInfo;
        var nickname = userInfo.nickName;
        var avatar = userInfo.avatarUrl;
        var sex = userInfo.gender;
        // 再次登录返回code
        wx.login({
          success: function(res) {
            // 获取需要上传的数据
            var code = res.code;
			  if (code != '' && nickname != '' && avatar!=''){
				  util.wGet('login', {
					  code: code,
					  nickname: nickname,
					  avatar: avatar,
					  sex: sex
				  }).then((res) => {
					  res = res.data;
					  wx.removeStorageSync('accessToken');
					  wx.setStorageSync('accessToken', 'Bearer' + ' ' + res.data.token);
					  app.globalData.getUserInfo = userInfo;
					  //根据传过来的index判断需要跳转的页面
					  util.wGet('user/info', {
					  }).then((res) => {
						  var res = res.data.data;
						  app.globalData.userId = res.id;
					  })
					  wx.hideToast();
					  if (index == '/pages/add/add') {
						  wx.navigateTo({
							  url: '/pages/add/add'
						  })
					  } else if (index == '/pages/user/user') {
						  wx.switchTab({
							  url: '/pages/user/user'
						  })
					  }

				  })
			  }
          }
        })
      }
      else if (e.detail.userInfo && getUserInfo != ''){
        // util.buttonClicked(this); 
        //第二次点击不需要授权
        //根据传过来的index判断需要跳转的页面
        if (index == '/pages/add/add') {
          wx.navigateTo({
            url: '/pages/add/add'
          })
        } else if (index == '/pages/user/user') {
          wx.switchTab({
            url: '/pages/user/user'
          })
        }
      } else {
        //用户按了拒绝按钮

      }
    },
    getIndex: function() {
      wx.switchTab({
        url: '/pages/index/index'
      })
    },

    // 判断登录超时或失效
    //在每个page onload的时候的回调
    userInfoCallback: function () {
      var that = this;
      var token = wx.getStorageSync("token");
      return new Promise(function (resolve, reject) {
        if (that.globalData.user) {
          resolve();
        } else {
          wx.request({
            url: that.appData.url + 'user',
            header: {
              'content-type': that.appData.contentType,
              'Accept': that.appData.contentType,
              'Authorization': 'Bearer ' + token
            },
            success: function (res) {
              if (res.statusCode == 200) {
                that.globalData.user = res.data.data;
                // wx.setStorageSync('user', res.data.data);
                resolve();
              } else {
                //没有权限，登录状态被清除或者已过期,重新登录
                wx.login({
                  success: res => {
                    wx.request({
                      url: that.appData.url + 'login',
                      header: {
                        'content-type': that.appData.contentType
                      },
                      data: {
                        code: res.code
                      },
                      method: 'POST',
                      success(res) {
                        // wx.setStorageSync('user', res.data.data.user);
                        that.globalData.user = res.data.data;
                        wx.setStorageSync('token', res.data.data.token);
                        resolve();
                      },
                    });
                  }
                });
              }
            }
          });
        }
      });
    }
  }
})
